package com.healthcare;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import webservices.HealthCareServerProxy;
import webservices.User;
import webservices.Doctor;

;

/**
 * Servlet implementation class oginServlet
 */
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Login() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession();

		System.out.println("logout");
		if (session != null) {
			
			session.removeAttribute("user");
			session.invalidate();
			HttpServletResponse httpResponse = (HttpServletResponse) response;

			httpResponse.setHeader("Cache-Control",
					"no-cache, no-store, must-revalidate");
			httpResponse.setHeader("Pragma", "no-cache");
			httpResponse.setDateHeader("Expires", 0);

			
			System.out.println(request.getSession().getAttribute("user"));
		
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/Home.jsp");
			dispatcher.forward(request, response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String username = request.getParameter("email");
		String password = request.getParameter("password");
		String userfirstname = null;
		User user = null;
		Doctor doctor = null;
		String inputlabel = null;
		String inputquestion = null;
		HttpSession session = request.getSession(true);
		HealthCareServerProxy proxy = new HealthCareServerProxy();
		

		if ((request.getParameter("login") != null)) {
			user = proxy.login(username, password);
			if (user != null) {

				session.setAttribute("user", user.getFirstName());
				session.setAttribute("userid", user.getUserId());
				session.setAttribute("username", user.getFirstName());
				session.setAttribute("email", user.getEmail());
				System.out.println("inside login");
				
				System.out.println("session" + session.getAttribute("user"));
				String userType = user.getUserType();
				System.out.println(userType);
				
					System.out.println("inside user");
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/Landing.jsp");
				dispatcher.forward(request, response);
				return;
			} else {
				doctor = proxy.doclogin(username, password);
				if (doctor != null) 
				{
				session.setAttribute("doctor", doctor.getFirstname());
				session.setAttribute("docid", doctor.getDocid());
				session.setAttribute("docname", doctor.getFirstname());
				session.setAttribute("docemail", doctor.getEmail());
				RequestDispatcher dispatcher = request
						.getRequestDispatcher("/DocLandingPage.jsp");
	
				dispatcher.forward(request, response);
				}
				else
				{
					RequestDispatcher dispatcher = request
							.getRequestDispatcher("/Home.jsp");
				request.setAttribute("error", true);
					dispatcher.forward(request, response);
				}
			return;
			}
		}
			
			else if ((request.getParameter("question") != null))
			inputlabel = request.getParameter("label");
		inputquestion = request.getParameter("question");
	
		System.out.println("username in post" + "M");
		DateFormat dateFormat = new SimpleDateFormat(
				"EEE, d MMM yyyy HH:mm:ss Z");
		Date date = new Date();

		userfirstname = (String) session.getAttribute("user");

		String status = proxy.postQues(inputlabel, inputquestion,
				userfirstname, dateFormat.format(date));
		System.out.println(status);
		String staticStatus = "Post is added!!";
		request.setAttribute("postStatus", status);
		if (status.equals(staticStatus)) {
			
			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/ListPosts.jsp");


			dispatcher.forward(request, response);
			System.out.println("after the insert");
		} else {

			RequestDispatcher dispatcher = request
					.getRequestDispatcher("/PostQuestion.jsp");

		
			dispatcher.forward(request, response);
			System.out.println("after no insert");
		}

		return;
	}

}
